package com.qsmx.qingqiuticket.app.delegate.back;

import android.hardware.usb.UsbDevice;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import androidx.annotation.Nullable;

import com.qsmx.core.delegates.AtmosDelegate;
import com.qsmx.core.util.StringUtil;
import com.qsmx.core.util.storage.AtmosPreference;
import com.qsmx.qingqiuticket.app.R;
import com.qsmx.qingqiuticket.app.databinding.DelegateOtherSettingBinding;
import com.qsmx.qingqiuticket.app.delegate.adapter.AppTypeSpanAdapter;
import com.qsmx.qingqiuticket.app.event.QrScanEvent;
import com.qsmx.qingqiuticket.app.fusion.FusionCode;
import com.qsmx.qingqiuticket.app.fusion.FusionData;
import com.qsmx.qingqiuticket.app.fusion.FusionPre;
import com.qsmx.qingqiuticket.app.fusion.FusionTag;
import com.qsmx.qingqiuticket.app.util.SoundPlayUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import ZtlApi.ZtlManager;

public class BackOtherSettingDelegate extends AtmosDelegate {

    private String selectedGpioPort = "";
    private DelegateOtherSettingBinding mBinding;

    @Override
    public Object setLayout() {
        return R.layout.delegate_other_setting;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        FusionCode.SCAN_TYPE = FusionCode.QR_SCANNER;
        FusionCode.QrIsLoading = false;
        mBinding = DelegateOtherSettingBinding.bind(rootView);
//        initGpioSpanInfo();
        mBinding.tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportDelegate().pop();
            }
        });

//        mBinding.tvGetGpioStatus.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                int gpioStatus = ZtlManager.GetInstance().getGpioValue(selectedGpioPort, "out");
//                if (gpioStatus == -1) {
//                    mBinding.tvGpioStatus.setText("IO口输入错误或IO口打开失败");
//                } else if (gpioStatus == -2) {
//                    mBinding.tvGpioStatus.setText("系统不支持该接口");
//                } else if (gpioStatus == 1) {
//                    mBinding.tvGpioStatus.setText("输入模式高电平");
//                } else if (gpioStatus == 2) {
//                    mBinding.tvGpioStatus.setText("输入模式低电平");
//                } else if (gpioStatus == 3) {
//                    mBinding.tvGpioStatus.setText("输出模式高电平");
//                } else if (gpioStatus == 4) {
//                    mBinding.tvGpioStatus.setText("输出模式低电平");
//                }
//            }
//        });
//
//        mBinding.tvHighGpio.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ZtlManager.GetInstance().setGpioValue(selectedGpioPort, 1);
//            }
//        });
//
//        mBinding.tvLowGpio.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ZtlManager.GetInstance().setGpioValue(selectedGpioPort, 0);
//            }
//        });

        mBinding.tvPlayRadio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SoundPlayUtils.play(1);
            }
        });
    }

    private void initGpioSpanInfo() {
        List<String> mGpioList = new ArrayList<>();
        mGpioList.add("序号2||GPIO2_A5");
        mGpioList.add("序号3||GPIO2_A3");
        mGpioList.add("序号6||GPIO2_A6");
        mGpioList.add("序号8||GPIO2_A4");
        mGpioList.add("序号13||GPIO2_A2 ");
        mGpioList.add("请选择");

        //适配器
        AppTypeSpanAdapter<String> arrAdapter = new AppTypeSpanAdapter<String>(requireContext(), R.layout.simple_spinner_item, mGpioList);
        //设置样式
        arrAdapter.setDropDownViewResource(R.layout.dropdown_stytle);
        //加载适配器
        mBinding.spGpioPort.setAdapter(arrAdapter);
        mBinding.spGpioPort.setSelection(mGpioList.size() - 1, true);
        mBinding.spGpioPort.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                selectedGpioPort = mGpioList.get(position).split("\\|\\|")[1];
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onQrScanEvent(QrScanEvent event) {
        if (isVisible()) {
            if (event != null && event.getData() != null) {
                FusionCode.SCAN_TYPE = FusionCode.QR_SCANNER;
                FusionCode.QrIsLoading = false;
                mBinding.tvScanInfo.setText("");
                String qrCode = event.getData().getString(FusionTag.QR_CODE);
                //todo 查询会员卡
                mBinding.tvScanInfo.setText(qrCode);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        SoundPlayUtils.release(1);
        FusionCode.SCAN_TYPE = FusionCode.CANCEL;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }
}
